package com.senlingspace.system.service.impl;

import cn.hutool.core.lang.Validator;
import com.senlingspace.common.core.domain.AjaxResult;
import com.senlingspace.common.utils.DateUtils;
import com.senlingspace.common.utils.StringUtils;
import com.senlingspace.system.domain.SlkjCustomerAddress;
import com.senlingspace.system.domain.dto.AreaDTO;
import com.senlingspace.system.mapper.SlkjCustomerAddressMapper;
import com.senlingspace.system.mapper.SysAreaMapper;
import com.senlingspace.system.service.ISlkjCustomerAddressService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

/**
 * 客户地址表 业务层实现类
 *
 * @author lebronjames
 * @date 2024/10/02
 */
@AllArgsConstructor
@Service
public class SlkjCustomerAddressServiceImpl implements ISlkjCustomerAddressService {

    private final SlkjCustomerAddressMapper slkjCustomerAddressMapper;
    private final SysAreaMapper sysAreaMapper;


    @Override
    public List<SlkjCustomerAddress> selectAllByCustomerId(Long customerId) {
        return slkjCustomerAddressMapper.selectByCustomerId(customerId);
    }

    @Override
    public SlkjCustomerAddress getDefault(Long customerId) {
        return slkjCustomerAddressMapper.getDefaultAddress(customerId);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public AjaxResult setDefault(Long addressId, Long customerId) {
        int updatedCount = slkjCustomerAddressMapper.resetDefaultAddress(customerId);
        if (updatedCount < 1) {
            return AjaxResult.error("更新失败！");
        }

        int result = slkjCustomerAddressMapper.setDefaultAddress(customerId, addressId);
        if (result == 1) {
            return AjaxResult.success("设置成功！");
        }
        return AjaxResult.error("设置失败！");
    }

    @Override
    public AjaxResult insert(SlkjCustomerAddress address) {
        String countyCode = address.getCountyCode();
        if (StringUtils.isBlank(countyCode)) {
            return AjaxResult.error("请选择地址！");
        }

        AreaDTO area = sysAreaMapper.findAreaByDistrictId(countyCode);
        if (area == null) {
            return AjaxResult.error("请选择正确的地址！");
        }

        if (StringUtils.isBlank(address.getAddress())) {
            return AjaxResult.error("详细地址不能为空！");
        }

        if (StringUtils.isBlank(address.getReceiverName()) || StringUtils.isBlank(address.getReceiverPhone())) {
            return AjaxResult.error("请输入姓名和手机号！");
        }

        if (!Validator.isMobile(address.getReceiverPhone())) {
            return AjaxResult.error("请输入正确的手机号！");
        }

        address.setProvinceCode(area.getProvinceId() + "");
        address.setCityCode(area.getCityId() + "");
        String addr = area.getFullName().replaceAll("，", "");
        address.setDetailAddress(addr + address.getAddress().trim());
        address.setAddress(address.getAddress().trim());
        address.setIsDefault(0); // 非默认地址
        int insert = slkjCustomerAddressMapper.insert(address);
        return insert == 1 ? AjaxResult.success("新增成功！") : AjaxResult.error("新增失败！");
    }

    @Override
    public AjaxResult deleteById(Long addressId, Long customerId) {
        SlkjCustomerAddress defaultAddress = slkjCustomerAddressMapper.getDefaultAddress(customerId);
        if (Objects.nonNull(defaultAddress) && defaultAddress.getAddressId().equals(addressId)) {
            return AjaxResult.warn("默认地址禁止删除！");
        }

        int delete = slkjCustomerAddressMapper.deleteById(addressId);
        return delete == 1 ? AjaxResult.success("删除成功！") : AjaxResult.error("删除失败！");
    }

    @Override
    public AjaxResult updateById(SlkjCustomerAddress address) {
        String countyCode = address.getCountyCode();
        if (StringUtils.isBlank(countyCode)) {
            return AjaxResult.error("请选择地址！");
        }

        AreaDTO area = sysAreaMapper.findAreaByDistrictId(countyCode);
        if (area == null) {
            return AjaxResult.error("请选择正确的地址！");
        }

        if (StringUtils.isBlank(address.getAddress())) {
            return AjaxResult.error("详细地址不能为空！");
        }

        if (StringUtils.isBlank(address.getReceiverName()) || StringUtils.isBlank(address.getReceiverPhone())) {
            return AjaxResult.error("请输入姓名和手机号！");
        }

        if (!Validator.isMobile(address.getReceiverPhone())) {
            return AjaxResult.error("请输入正确的手机号！");
        }

        address.setProvinceCode(area.getProvinceId() + "");
        address.setCityCode(area.getCityId() + "");
        String addr = area.getFullName().replaceAll("，", "");
        address.setDetailAddress(addr + address.getAddress().trim());
        address.setAddress(address.getAddress().trim());
        address.setUpdateTime(DateUtils.getNowDate());
        int update = slkjCustomerAddressMapper.updateById(address);
        return update == 1 ? AjaxResult.success("修改成功！") : AjaxResult.error("修改失败！");
    }

}
